<?php
namespace modules\system\service;

use idea\saf\sl\Service;
use helper\ErrorNo;
use helper\ErrorMsg;
use helper\Log;
use modules\system\form;

/**
 * Options class file
 * options表服务类
 * @author 宋欢 <iphper@yeah.net>
 * @version $Id: Options.php 1 2013-05-18 14:58:59Z huan.song $
 * @package modules.system.service
 * @since 1.0
 */
class Options extends Service
{
	/**
	 * 通过类别ID保存站点配置
	 * @param integer $category
	 * @param array $params
	 * @return array
	 */
	public function save($category, array $params = array())
	{
		$ret = self::findAllByCategory($category);
		if ($ret['err_no'] !== ErrorNo::SUCCESS_NUM) {
			return $ret;
		}

		$data = array();
		foreach ($ret['data'] as $row) {
			$data[$row['option_key']] = array(
				'id' => $row['option_id'],
				'value' => $row['option_value']
			);
		}
		
		$filters = form\Options::$data[$category];
		foreach ($params as $key => $value) {
			if (!in_array($key, $filters)) {
				continue;
			}

			$id = isset($data[$key]) ? $data[$key]['id'] : null;
			$attributes = array(
				'option_id' => $id,
				'option_key' => $key,
				'option_value' => $value,
				'option_category' => $category
			);
			$ret = $this->getDao()->replace($attributes);
		}
		
		$this->fileDelete();
		return self::findAllByCategory($category);
	}

	/**
	 * 获取所有的站点配置，如果存在缓存文件，从缓存文件中获取
	 * @return array
	 */
	public function findAll()
	{
		if ($this->getForm()->fileExists()) {
			return $this->fileFindAll();
		}

		$ret = $this->findPairs();
		if ($ret['err_no'] !== ErrorNo::SUCCESS_NUM) {
			return $ret;
		}
		
		$this->fileUpdate($ret['data']);
		return $ret;
	}

	/**
	 * 获取所有的站点配置
	 * @return array
	 */
	public function findPairs()
	{
		$ret = parent::findAll();
		if ($ret['err_no'] !== ErrorNo::SUCCESS_NUM) {
			return $ret;
		}

		$data = array();
		foreach ($ret['data'] as $row) {
			$data[$row['option_key']] = $row['option_value'];
		}
		
		$ret['data'] = $data;
		return $ret;
	}

	/**
	 * 通过类别ID，获取所有的站点配置
	 * @param integer $value
	 * @return array
	 */
	public function findAllByCategory($value)
	{
		$value = (int) $value;
		if (!isset(form\Options::$data[$value])) {
			$errNo = ErrorNo::ERROR_ARGS_SELECT;
			$errMsg = ErrorMsg::ERROR_ARGS_SELECT;
			Log::warning(sprintf(
				'%s category "%d" not exists', $errMsg, $value
			), $errNo, __METHOD__);
			return array(
				'err_no' => $errNo,
				'err_msg' => $errMsg
			);
		}
		
		$ret = parent::findAllByAttributes(array(
			'option_category' => $value
		));
		return $ret;
	}
	
	/**
	 * 从文件缓存中，获取所有的站点配置
	 * @return array
	 */
	public function fileFindAll()
	{
		$form = $this->getForm();
		$filePath = $form->getFilePath();
		if (!$form->fileExists()) {
			$errNo = ErrorNo::ERROR_FILE_SELECT;
			$errMsg = ErrorMsg::ERROR_FILE_SELECT;
			Log::warning(sprintf(
				'%s file path "%s" not exists', $errMsg, $filePath
			), $errNo, __METHOD__);
			return array(
				'err_no' => $errNo,
				'err_msg' => $errMsg
			);
		}
		
		$data = require_once $filePath;
		if (!is_array($data) || empty($data)) {
			$errNo = ErrorNo::ERROR_FILE_SELECT;
			$errMsg = ErrorMsg::ERROR_FILE_SELECT;
			Log::warning(sprintf(
				'%s config empty by file path "%s"', $errMsg, $filePath
			), $errNo, __METHOD__);
			return array(
				'err_no' => $errNo,
				'err_msg' => $errMsg
			);
		}

		$errNo = ErrorNo::SUCCESS_NUM;
		$errMsg = ErrorMsg::SUCCESS_SELECT;
		Log::notice(sprintf(
			'%s file path "%s"', $errMsg, $filePath
		), __METHOD__);

		return array(
			'err_no' => $errNo,
			'err_msg' => $errMsg,
			'data' => $data
		);
	}
	
	/**
	 * 更新缓存文件，保存站点配置
	 * @param array $data
	 * @return array
	 */
	public function fileUpdate(array $data = array())
	{
		if (empty($data)) {
			$errNo = ErrorNo::ERROR_ARGS_UPDATE;
			$errMsg = ErrorMsg::ERROR_ARGS_UPDATE;
			Log::warning(sprintf(
				'%s data empty', $errMsg
			), $errNo, __METHOD__);
			return array(
				'err_no' => $errNo,
				'err_msg' => $errMsg
			);
		}
		
		$filePath = $this->getForm()->getFilePath();
		$codeText = "<?php \nreturn " . var_export($data, true) . ";";
		if (file_put_contents($filePath, $codeText) <= 0) {
			$errNo = ErrorNo::ERROR_FILE_UPDATE;
			$errMsg = ErrorMsg::ERROR_FILE_UPDATE;
			Log::warning(sprintf(
				'%s file path "%s"', $errMsg, $filePath
			), $errNo, __METHOD__);
			return array(
				'err_no' => $errNo,
				'err_msg' => $errMsg
			);
		}
		
		$errNo = ErrorNo::SUCCESS_NUM;
		$errMsg = ErrorMsg::SUCCESS_UPDATE;
		Log::notice(sprintf(
			'%s file path "%s"', $errMsg, $filePath
		), __METHOD__);
		return array(
			'err_no' => $errNo,
			'err_msg' => $errMsg
		);
	}

	/**
	 * 删除站点配置缓存文件
	 * @return array
	 */
	public function fileDelete()
	{
		$form = $this->getForm();
		$filePath = $form->getFilePath();
		
		if (!$form->fileDelete()) {
			$errNo = ErrorNo::ERROR_FILE_DELETE;
			$errMsg = ErrorMsg::ERROR_FILE_DELETE;
			Log::warning(sprintf(
				'%s file path "%s"', $errMsg, $filePath
			), $errNo, __METHOD__);
			return array(
				'err_no' => $errNo,
				'err_msg' => $errMsg
			);
		}

		$errNo = ErrorNo::SUCCESS_NUM;
		$errMsg = ErrorMsg::SUCCESS_DELETE;
		Log::notice(sprintf(
			'%s file path "%s"', $errMsg, $filePath
		), __METHOD__);
		return array(
			'err_no' => $errNo,
			'err_msg' => $errMsg
		);
	}
}
